<?php
class ControllerSaleVoucher extends AdminController {

  	public function index() {
		M('sale/voucher');
		$this->getList();
  	}

  	public function insert() {
		M('sale/voucher');

    	if ($this->request->isPost() && $this->validateForm()) {
			$this->model_sale_voucher->addVoucher($this->request->post);

			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('sale/voucher'));
    	}

    	$this->getForm();
  	}

  	public function update() {

		M('sale/voucher');

    	if ($this->request->isPost() && $this->validateForm()) {
			$this->model_sale_voucher->editVoucher($this->request->get['voucher_id'], $this->request->post);

			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('sale/voucher'));
		}

    	$this->getForm();
  	}

  	public function delete() {

		M('sale/voucher');

    	if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $voucher_id) {
				$this->model_sale_voucher->deleteVoucher($voucher_id);
			}

			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('sale/voucher'));
    	}

    	$this->getList();
  	}

  	protected function getList() {
		$this->document->setTitle($this->_['heading_title']);

		$qf = new Query_filter();

		$this->data['vouchers'] = array();

		$voucher_total = $this->model_sale_voucher->getTotalVouchers();

		$results = $this->model_sale_voucher->getVouchers($qf);

    	foreach ($results as $result) {
			$action = array();

			$action[] = array(
				'text' => $this->_['text_edit'],
				'href' => UA('sale/voucher/update', 'voucher_id=' . $result['voucher_id'])
			);

			$this->data['vouchers'][] = array(
				'voucher_id' => $result['voucher_id'],
				'code'       => $result['code'],
				'from'       => $result['from_name'],
				'to'         => $result['to_name'],
				'theme'      => $result['theme'],
				'amount'     => $this->currency->format($result['amount'], C('config_currency')),
				'status'     => ($result['status'] ? $this->_['text_enabled'] : $this->_['text_disabled']),
				'date_added' => v5_date_format($result['date_added']),
				'action'     => $action
			);
		}

		$this->data['pagination'] = $this->pagination($voucher_total)->render();

		$this->display('sale/voucher_list.tpl');
  	}

  	protected function getForm() {
		$this->document->setTitle($this->_['heading_title']);

		if (isset($this->request->get['voucher_id'])) {
			$this->data['voucher_id'] = $this->request->get['voucher_id'];
			$this->data['action'] = UA('sale/voucher/update', 'voucher_id=' . $this->request->get['voucher_id']);
		} else {
			$this->data['voucher_id'] = 0;
			$this->data['action'] = UA('sale/voucher/insert');
		}

		if (isset($this->request->get['voucher_id']) && !$this->request->isPost()) {
      		$voucher_info = $this->model_sale_voucher->getVoucher($this->request->get['voucher_id']);
    	}

    	if (!empty($voucher_info)) {
			$this->data['code'] = $voucher_info['code'];
			$this->data['from_name'] = $voucher_info['from_name'];
			$this->data['from_email'] = $voucher_info['from_email'];
			$this->data['to_name'] = $voucher_info['to_name'];
			$this->data['to_email'] = $voucher_info['to_email'];
			$this->data['voucher_theme_id'] = $voucher_info['voucher_theme_id'];
			$this->data['message'] = $voucher_info['message'];
			$this->data['amount'] = $voucher_info['amount'];
			$this->data['status'] = $voucher_info['status'];
		} else {
      		$this->data['code'] = P('code');
			$this->data['from_name'] = P('from_name');
			$this->data['from_email'] = P('from_email');
			$this->data['to_name'] = P('to_name');
			$this->data['to_email'] = P('to_email');
			$this->data['voucher_theme_id'] = P('voucher_theme_id');
			$this->data['message'] = P('message');
			$this->data['amount'] = P('amount');
			$this->data['status'] = P('status', 1);
    	}

 		M('sale/voucher_theme');
		$this->data['voucher_themes'] = $this->model_sale_voucher_theme->getVoucherThemes();

		$this->display('sale/voucher_form.tpl');
  	}

  	protected function validateForm() {

		$pass = true;
    	if ((utf8_strlen($this->request->post['code']) < 3) || (utf8_strlen($this->request->post['code']) > 10)) {
      		$this->setMessage('error_code', $this->_['error_code']);
			$pass = false;
    	}

		$voucher_info = $this->model_sale_voucher->getVoucherByCode($this->request->post['code']);

		if ($voucher_info) {
			if (!isset($this->request->get['voucher_id']) || $voucher_info['voucher_id'] != $this->request->get['voucher_id']) {
				$this->setMessage('error_warning', $this->_['error_exists']);
				$pass = false;
			}
		}

    	if ((utf8_strlen($this->request->post['to_name']) < 1) || (utf8_strlen($this->request->post['to_name']) > 64)) {
      		$this->setMessage('error_to_name', $this->_['error_to_name']);
			$pass = false;
    	}

		if ((utf8_strlen($this->request->post['to_email']) > 96) || !preg_match('/^[^\@]+@.*\.[a-z]{2,6}$/i', $this->request->post['to_email'])) {
      		$this->setMessage('error_to_email', $this->_['error_email']);
			$pass = false;
    	}

    	if ((utf8_strlen($this->request->post['from_name']) < 1) || (utf8_strlen($this->request->post['from_name']) > 64)) {
      		$this->setMessage('error_from_name', $this->_['error_from_name']);
			$pass = false;
    	}

		if ((utf8_strlen($this->request->post['from_email']) > 96) || !preg_match('/^[^\@]+@.*\.[a-z]{2,6}$/i', $this->request->post['from_email'])) {
      		$this->setMessage('error_from_email', $this->_['error_email']);
			$pass = false;
    	}

		if ($this->request->post['amount'] < 1) {
      		$this->setMessage('error_amount', $this->_['error_amount']);
			$pass = false;
    	}

    	return $pass;
  	}

  	protected function validateDelete() {

		M('sale/order');

		foreach ($this->request->post['selected'] as $voucher_id) {
			$order_voucher_info = $this->model_sale_order->getOrderVoucherByVoucherId($voucher_id);

			if ($order_voucher_info) {
				$this->setMessage('error_warning', sprintf($this->_['error_order'], UA('sale/order/info', 'order_id=' . $order_voucher_info['order_id'])));
				return false;
			}
		}

		return true;
  	}

	public function history() {

		M('sale/voucher');

		$page = G('page', 1);

		$this->data['histories'] = array();

		$results = $this->model_sale_voucher->getVoucherHistories($this->request->get['voucher_id'], ($page - 1) * 10, 10);

		foreach ($results as $result) {
        	$this->data['histories'][] = array(
				'order_id'   => $result['order_id'],
				'customer'   => $result['customer'],
				'amount'     => $this->currency->format($result['amount'], C('config_currency')),
        		'date_added' => date($this->_['date_format_short'], strtotime($result['date_added']))
        	);
      	}

		$history_total = $this->model_sale_voucher->getTotalVoucherHistories($this->request->get['voucher_id']);

		$url = UA('sale/voucher/history', 'voucher_id=' . $this->request->get['voucher_id'] . '&page={page}');
		$this->data['pagination'] = $this->pagination($history_total, $url, 10, $page)->render();

		$this->display('sale/voucher_history.tpl');
  	}

	public function send() {

		$json = array();

     	if (!$this->hasMP()) {
      		$json['error'] = $this->_['error_permission'];
    	} elseif (isset($this->request->get['voucher_id'])) {
			M('sale/voucher');

			$this->model_sale_voucher->sendVoucher($this->request->get['voucher_id']);

			$json['success'] = $this->_['text_sent'];
		}

		$this->response->setOutput(json_encode($json));
  	}
}
?>